package com.jiuwan.gamepass.service.userImpl;

import com.jiuwan.gamepass.dto.authDto.Result;
import com.jiuwan.gamepass.dto.authDto.ResultCode;
import com.jiuwan.gamepass.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-07-01 09:56
 */
@Component
public class AdminServiceImpl {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    public Result login(String username, String password, HttpServletRequest request){
        //登录
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (null==userDetails || !passwordEncoder.matches(password,userDetails.getPassword())){
            return  Result.error(ResultCode.USER_CREDENTIALS_ERROR);
        }

        //更新security登录用户对象   把登录对象放到全局上下文中
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);

        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        Result ok = Result.ok();
        ok.setData(tokenMap);
        return ok;
    }

    public Result loginCaptcha(String username, String password, String code,HttpServletRequest request) {
        if(username ==null || password==null || code==null){
            return Result.error(ResultCode.PARAM_NOT_COMPLETE);
        }
        if (request.getSession().getAttribute("captcha")==null){
            return Result.error(ResultCode.PARAM_CAPTCHA_NUll);
        }
        String captcha = (String) request.getSession().getAttribute("captcha");
        if (StringUtils.isEmpty(code)|| !captcha.equalsIgnoreCase(code)){
            return Result.error(ResultCode.PARAM_CAPTCHA_NOT_COMPLETE);
        }

        //登录
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (null==userDetails || !passwordEncoder.matches(password,userDetails.getPassword())){
            return  Result.error(ResultCode.USER_CREDENTIALS_ERROR);
        }

        //更新security登录用户对象   把登录对象放到全局上下文中
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);

        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        Result ok = Result.ok();
        ok.setData(tokenMap);
        return ok;
    }
}
